package com.shixun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shixun.controller.vo.SaveVO;
import com.shixun.domain.Board;
import com.shixun.domain.BusinessException;
import com.shixun.mapper.B;
import com.shixun.service.BoardService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/board")
@CrossOrigin
@Slf4j
public class BoardController {
    @Resource
    private BoardService boardService;

    @GetMapping("/queryById/{id}")
    public B queryById(@PathVariable Integer id) {
        Board board = boardService.queryById(id);

        B b = new B(200, "1", board);
        return b;
    }

    @DeleteMapping("/deleteBoard/{id}")
    public B deleteBoard(@PathVariable Integer id) {
        boardService.deleteBoard(id);
        B b = new B(200, "删除成功", null);
        return b;
    }

    @PostMapping("/saveBoard")
    public B saveBoard(@Valid @RequestBody SaveVO saveVO) throws BusinessException {
        Board board = new Board();
        BeanUtils.copyProperties(saveVO, board);
        boardService.saveBoard(board);
        B b = new B(200, "添加成功", null);
        return b;
    }

    @GetMapping("/queryBoards")
    public B queryBoards(int pageNum,int pageSize) {
        Page<Board> page=new Page<>(pageNum,pageSize);
        boardService.page(page);
//        List<Board> boards = boardService.queryBoards();
        B b = new B(200, "cg",page);
        return b;
    }

    @PutMapping("/updateBoard")
    public B updateBorad(@RequestBody(required = false) Board board) {
        System.out.println("111111111111111111111111111111111111111111");
        log.info("bord对象为{}",board);
        Board board1=new Board();
        board1.setId(board.getId());
        board1.setMessage(board.getMessage());


        boardService.updateById(board1);
        B b = new B(200, "cg", null);
        return b;
    }
}
